package com.baoji.auth.service.impl;

import com.baoji.auth.entity.Role;
import com.baoji.auth.entity.User;
import com.baoji.auth.mapper.UserMapper;
import com.baoji.auth.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Auther: ghm
 * @Date: 2021/09/26/11:26
 * @Description:
 */

@Service
public class UserServiceImpl implements UserService {

    @Autowired //密码编码器
    private BCryptPasswordEncoder bCryptPasswordEncoder;

    @Autowired
    UserMapper userMapper;

    @Transactional
    @Override
    public void save(String username, String password) {

        // 添加用户
        User user = new User();
        user.setUsername(username);
        // 密码加密
        String encode = bCryptPasswordEncoder.encode(password);
        user.setPassword(encode);
        userMapper.insert(user);

        // 添加角色
        Role role = new Role();
        role.setUsername(username);
        role.setName("USER");
        userMapper.insertRole(role);

    }

    @Override
    public boolean cheakUser(String username) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        User user = userMapper.selectOne(queryWrapper);
        if (user == null) {
            return true;
        }
        return false;
    }
}
